package cn.com.nes.common.util;

import cn.com.nes.site.entity.PageData;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Page {
	int pageid ;
	int pagesize ;
	int pagecount ;
	int totalscount ;
	int offsetlimit ;
	
	public int getOffsetlimit() {
		return offsetlimit;
	}


	public void setOffsetlimit(int offsetlimit) {
		this.offsetlimit = offsetlimit;
	}


	public int getPageid() {
		return pageid;
	}


	public void setPageid(int pageid) {
		this.pageid = pageid;
	}


	public int getPagesize() {
		return pagesize;
	}


	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}


	public int getPagecount() {
		return pagecount;
	}


	public void setPagecount(int pagecount) {
		this.pagecount = pagecount;
	}


	public int getTotalscount() {
		return totalscount;
	}


	public void setTotalscount(int totalscount) {
		this.totalscount = totalscount;
	}


	public Page(JdbcTemplate conn,String sql,int currentpage,int pagesize,PageData vector, Object[] objs) throws SQLException{  
		StringBuffer sb = new StringBuffer();
		try {
			sb.append(" select count(1) cnt ").append(sql).append(" ") ;
			
			int recordcount  = conn.queryForObject(sb.toString(),objs, Integer.class);
			
			int pagecount = recordcount % pagesize ==0 ? recordcount/pagesize : recordcount/pagesize + 1 ;
			this.pageid = currentpage ;
			this.pagesize = pagesize ;
			this.totalscount = recordcount ;
			this.pagecount = pagecount ;
			this.offsetlimit = (this.pageid-1) * pagesize ; 
			
			vector.setPageid(this.getPageid());
			vector.setPagesize(this.getPagesize());
			vector.setPagecount(this.getPagecount());
			vector.setRecordcount(recordcount);
			
		} finally {
		}
	   }  
	
	public Page(Connection conn,String sql,int currentpage,int pagesize,PageData vector, Object[] objs) throws SQLException{  
		StringBuffer sb = new StringBuffer();
		try {
			sb.append(" select count(1) cnt ").append(sql).append(" ") ;
			
			int recordcount  = queryForInt(conn,sb.toString(),objs);
			
			int pagecount = (int) (new Double(recordcount) % pagesize ==0 ? new Double(recordcount)/pagesize : new Double(recordcount)/pagesize + 1) ;
			this.pageid = currentpage ;
			this.pagesize = pagesize ;
			this.totalscount = recordcount ;
			this.pagecount = pagecount ;
			this.offsetlimit = (this.pageid-1) * pagesize ; 
			
			vector.setPageid(this.getPageid());
			vector.setPagesize(this.getPagesize());
			vector.setPagecount(this.getPagecount());
			vector.setRecordcount(recordcount);
			
		} finally {
		}
	   }
	
	public int queryForInt(Connection conn, String sql, Object[] obj) {
		int cnt = 0;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			if (obj != null && obj.length > 0) {
				for (int i = 0; i < obj.length; i++) {
					ps.setObject(i + 1, obj[i]);
				}
			}
			rs = ps.executeQuery();
			if (rs.next()) {
				cnt = rs.getInt(1);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		return cnt;
	}
	
}
